package com.dreamchaser.familydevice.config;


import com.dreamchaser.familydevice.support.XssSqlStringJsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 *
 */
@Configuration
public class FastJsonConfiguration {

    /**
     * FastJson ObjectMapper
     *
     * @param builder
     * @return
     */
    @Bean
    /**
     * @Primary 可以使得Spring IOC 在可以注入多个Bean时 ，优先加载@Primary注解的Bean
     */
    @Primary
    public ObjectMapper xssObjectMapper(Jackson2ObjectMapperBuilder builder) {
        // 解析器
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        // 注册 XSS SQL解析器
        SimpleModule xssModule = new SimpleModule("XssStringJsonSerializer");
        xssModule.addSerializer(new XssSqlStringJsonSerializer());
        objectMapper.registerModule(xssModule);
        return objectMapper;
    }
}
